import originAxios from 'axios'
import {Message} from 'element-ui'
import router from "../router/index"
export default function axios(option) {
    return new Promise((resolve, reject) => {
        // 1.创建axios的实例
        const instance = originAxios.create({
            // baseURL: '/api',
            baseURL: process.env.VUE_APP_BASE_API,
            timeout: 10000
        });

        // 配置请求和响应拦截
        instance.interceptors.request.use(config => {
            if (!config.headers['Authorization']) {
                config.headers['Authorization'] = localStorage.getItem('token')
            }
            if (config.method === "POST" || config.method === "post") {
                if (!config.headers['Content-Type']) {
                    config.headers['Content-Type'] = `application/json`
                }
            } else if (config.method === "GET" || config.method === "get") {
                config.headers['Content-Type'] = `application/x-www-form-urlencoded`
            }

            if (config.method === '' || config.method === null) {
                config.method = 'GET'
            }

            // config.data = qs.parse(config.data)
            // 4.等等
            return config
        }, err => {
            // console.log('来到了request拦截failure中');
            return err
        })

        instance.interceptors.response.use(response => {
            if (response.status === 200 && response.data.code === 200) {
                return response.data
            } else {
                Message({
                    message: response.data.message ? response.data.message : '系统异常！',
                    type: "error",
                    duration: 3 * 1000
                })
                reject(response.data.msg)
            }
        }, error => {
            if (error.response) {
                if (error.response.status === 401) {
                    Message({
                        message: error.response.data.message,
                        type: 'error'
                    })
                  router.push("/login")
                    reject(error.response)
                } else if (error.response.status === 404) {
                    Message({
                        message: '数据接口不存在，请确认！',
                        type: 'error'
                    })
                    reject(error.response)
                } else if (error.response.status === 502) {
                    Message({
                        message: '网关错误，请检查服务是否正常！',
                        type: 'error'
                    })
                    reject(error.response)
                } else if (error.response.status === 403) {
                    Message({
                        message: '权限不足，请检查是否已授权访问该权限！',
                        type: 'error'
                    })
                    reject(error.response)
                } else if (error.response.status === 500) {
                    if (error.response.data && error.response.data.message) {
                        Message({
                            message: error.response.data.message,
                            type: 'error'
                        })
                    } else {
                        Message({
                            message: '服务异常！',
                            type: 'error'
                        })
                    }
                    reject(error.response)
                } else {
                    Message({
                        message: '系统异常！',
                        type: 'error'
                    })
                    reject(error.response)
                }
            } else {
                Message({
                    message: '系统异常！',
                    type: 'error'
                })
                reject(error.response)
            }
            reject(error)
        })

        // 2.传入对象进行网络请求
        instance(option).then(res => {
            resolve(res)
        }).catch(err => {
            reject(err)
        })
    })
}
